import numpy as np
import cv2

path = '../../../../large_data/pic/Lenna_.png'
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("img", img.shape)  # (512, 512)

cv2.imshow("img", img)

k = 30
U, S, V = np.linalg.svd(img)
P = V[:k, :]
z = np.dot(img, P.T)
print("z:", z.shape)

appro = np.dot(z, P)
appro = np.uint8(appro)
print("appro:", appro.shape)
cv2.imshow("appro", appro)
cv2.waitKey(0)
cv2.destroyAllWindows()
